package com.example.yiwei.crm.view.activity.customerActivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetCommunicateLogData;
import com.example.yiwei.crm.data.GetPersonnal;
import com.example.yiwei.crm.entity.CommunicateLogEntity;
import com.example.yiwei.crm.entity.personnalEntity;
import com.example.yiwei.crm.util.NetWorkUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class AddContactLogActivity extends AppCompatActivity {
    private EditText edit_theme,edit_content,edit_next_content;
    private TextView txt_next_date,txt_title;
    private Button btn_next_date;
    private ImageView img_back,img_commit;
    private TimePickerView mpv;
    int customer_id;
    String next_date;
    personnalEntity personnal;
    String result;
    int create_id;
    CommunicateLogEntity communicate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact_log);
        edit_content= (EditText) findViewById(R.id.add_contact_log_edit_content);
        edit_theme= (EditText) findViewById(R.id.add_contact_log_edit_theme);
        edit_next_content= (EditText) findViewById(R.id.add_contact_log_edit_next_content);
        txt_next_date= (TextView) findViewById(R.id.add_contact_log_txt_next_date);
        btn_next_date= (Button) findViewById(R.id.add_contact_log_btn_next_date);
        img_back= (ImageView) findViewById(R.id.add_contact_log_img_back);
        txt_title= (TextView) findViewById(R.id.add_contact_log_txt_title);
        img_commit= (ImageView) findViewById(R.id.add_contact_log_img_commit);
        communicate= GetCommunicateLogData.get();
        if ("communicate_update".equals(getIntent().getStringExtra("tag"))){
            txt_title.setText("修改沟通日志");
            edit_theme.setText(communicate.getCommunication_theme());
            edit_content.setText(communicate.getCommunication_content());
            edit_next_content.setText(communicate.getNext_communicate_content());
            if (communicate.getNext_communicate_date()!=null) {
                boolean b = communicate.getNext_communicate_date().matches("\\d{4}-\\d{2}-\\d{2}");
                if (b) {
                    txt_next_date.setText(communicate.getNext_communicate_date());
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd ");
                    sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                    String time2 = communicate.getNext_communicate_date();
                    long l = Long.parseLong(time2);
                    Date date = new Date(l);
                    String next_time = sdf.format(date);
                    txt_next_date.setText(next_time);
                }
            }
        }
        customer_id=getIntent().getExtras().getInt("customer_id");
        personnal= GetPersonnal.get();
        create_id=personnal.getPersonnal_id();
        //创建选择器
        mpv = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv.setTime(new Date());

        //选择监听
        mpv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                next_date=getTime(date);
                txt_next_date.setText(next_date);
            }
        });
        btn_next_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv.show();
            }
        });
       img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        img_commit.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                  add();
    }
});
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    public void add(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject object=new JSONObject();
                try {
                    if ("communicate_update".equals(getIntent().getStringExtra("tag"))){
                        object.put("UID01",create_id);
                    }else {
                        object.put("CID01", create_id);
                        object.put("KAA01", customer_id);
                    }
                    object.put("KGA02",edit_theme.getText().toString());
                    object.put("KGA03",edit_content.getText().toString());
                    object.put("KGA04",txt_next_date.getText().toString());
                    object.put("KGA05",edit_next_content.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json=object.toString();
                if ("communicate_update".equals(getIntent().getStringExtra("tag"))){
                    result=NetWorkUtils.getCount(baseClass.update_contact_log_url+getIntent().getExtras().getInt("KGA01"),json);
                }else{
                    result= NetWorkUtils.getCount(baseClass.add_contact_log_url,json);
                }
                try {
                    JSONObject object1=new JSONObject(result);
                    String boolen=object1.getString("boolean");
                    if ("success".equals(boolen)) {
                        finish();
                        if ("communicate_update".equals(getIntent().getStringExtra("tag"))){
                            DetailedCommunicateLogMessage.a.finish();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
